<?php
namespace My\Controller;

use Utuo\BackgroundController;

class MaterialController extends BackgroundController{
	
	// 初始化
	public function _initialize() {
		$this->setControllerTitle('素材管理');
		$this->setActionTitle(array(
				'index' => '素材列表',
				'add' =>'添加素材',
				'recycle' => '回收站',
				'modify' => '修改素材'
		));
		$this->setActionNav(array(
				'index',
				'add',
				'recycle'
		));
	}
	
	
	// 素材列表
	public function indexAction() {
		$this->getMaterialList(0);
	}

	
	// 获取列表
	private function getMaterialList($is_deleted) {
		$page = $this->getPage();
		$pagesize = 10;
		$condition = array(
			'is_deleted' => $is_deleted,
			'cid' => $this->aid
		);
		$materialLists = M('Material')->where($condition)->limit($pagesize)->page($page)->order('id DESC')->select();
		$materialCount = M('Material')->where($condition)->count();
		$this->assign('materialLists', $materialLists);
		$this->showPage($materialCount, $pagesize);
		$this->display();
	}
	
	// 添加素材页面
	public function addAction() {
		$this->display();
	}
	
	
	// 添加素材
	public function addPostAction() {
		$d = D('material');
		if ($d->create()) {
			$d->cid = $this->aid;
			$d->content = htmlspecialchars_decode($d->content);
			if ($d->add()) {
				$this->ajaxSuccess('添加素材成功');
			} else {
				$this->ajaxError($d->getError());
			}
		} else {
			$this->ajaxError('创建数据失败');
		}
	}
	
	
	// 修改页面
	public function modifyAction(){
		$id = I('id', 0, 'intval');
		$material = M('material')->getById($id);
		$this->checkAdmin($material['cid']);
		$material['content'] = htmlspecialchars_decode($material['content']);
		$this->assign('material', $material);
		$this->display();
	}
	
	
	// 提交修改
	public function modifyPostAction() {
		$d = D('material');
		if ($d->create()) {
			$this->checkAdmin(null, $d, "cid");
			$d->cid = $this->aid;
			$d->content = htmlspecialchars_decode($d->content);
			if (false !== $d->save()) {
				$this->ajaxSuccess('修改素材成功');
			} else {
				$this->ajaxError($d->getError());
			}
		} else {
			$this->ajaxError($d->getError());
		}
	}
	
	
	// 文件上传
	public function uploadAction() {
		$upload = new \Utuo\Upload();
		$info = $upload->uploadOne();
		if (!$info) {
			$this->ajaxError($upload->getError());
		}else{
			$this->ajaxSuccess("上传成功", $info);
		}
	}
	
	
	// 移除菜单
	public function removePostAction() {
		$this->ajaxRemove();
	}
	
	
	// 恢复菜单
	public function restorePostAction(){
		$this->ajaxRestore();
	}
	
	
	// 彻底删除
	public function deletePostAction(){
		$this->ajaxDelete();
	}
	
	
	// 回收站
	public function recycleAction() {
		$this->getMaterialList(1);
	}
	
	// 获得素材列表
	public function getNewsListPostAction() {
		$p = I('get.p',1,'intval');
		$pageSize = 10;
		$d = M("Material")->field("id,title,cover_url")->where(array('cid' => $this->aid, 'is_deleted' => 0));
		$materialLists = $d->limit(($p-1)*$pageSize.','.$pageSize)->order("id desc")->select();
		if ($materialLists || !$d->getError()) {
			$this->ajaxSuccess('', $materialLists);
		} else {
			$this->ajaxError("获取图文列表失败");
		}
	}
	
	
}